if not index then

dofile (ScriptsRootPath .. "lib\\lib.lua")


index = {
	all_trades = {
	}
}


function index:load()
	if not self.all_trades.min or self.date ~= lib:FormatDate() then
		self.date = lib:FormatDate()
		self.all_trades.min = {}
		self.table_index = nil
		
		if lib:GetUser() then
			local file = lib.path.index .. "all_trades.min." .. self.date .. "." .. lib:GetUser()
			local data, d, min, last
			
			local hf = io.open(file, "r")
			
			if hf then
				data = hf:read("*a")
				hf:close()
				
				if data then
					for v in string.gmatch(data, "([^\n]+)\n") do
						d = split(v, "\t")
						
						if #d >= 3 then
							min = int(string.sub(d[1], 1, 2)) * 60 + int(string.sub(d[1], 3, 4))
							
							if not last or last.min < min then
								table.sinsert(self.all_trades.min, tostring(min))
								last = {
										index = #self.all_trades.min,
										time = d[1],
										min = min,
										first = int(d[2]),
										last = int(d[3]),
										--low = tonumber(d[4]),
										--high = tonumber(d[5]),
										loaded = true
									}
								self.all_trades.min[tostring(min)] = last
							end
						end
					end
					
					local t1, t2
					
					for i = #self.all_trades.min - 1, #self.all_trades.min do
						if i > 0 then
							d = self.all_trades.min[ self.all_trades.min[i] ]
							t1 = getItem("all_trades", d.first)
							t2 = getItem("all_trades", d.last)
							
							if not t1 or not t2
								or d.min ~= int(t1.datetime.hour * 60 + t1.datetime.min)
								or d.min ~= int(t2.datetime.hour * 60 + t2.datetime.min)
							then
								self.all_trades.min = {}
								lib:remove(file)
								break
							end
						end
					end
				end
			end
		end
	end
end


function index:save()
	if #self.all_trades.min > 0 then
		local file = lib.path.index .. "all_trades.min." .. self.date .. "." .. lib:GetUser()
		
		if not lib:exists(file) then
			for i, v in ipairs(self.all_trades.min) do
				self.all_trades.min[v].loaded = nil
			end
		end
		
		local hf = io.open(file, "a")
		
		if hf then
			for i, v in ipairs(self.all_trades.min) do
				if not self.all_trades.min[v].loaded then
					hf:write(string.format("%s\t%u\t%u\n",
						self.all_trades.min[v].time,
						self.all_trades.min[v].first,
						self.all_trades.min[v].last
						--self.all_trades.min[v].low,
						--self.all_trades.min[v].high
					))
					self.all_trades.min[v].loaded = true
				end
			end
			
			hf:close()
		end
	end
end


function index:AddTrade(trade, table_index)
	if trade and type(trade) == "table"
		and trade.sec_code
		and table_index
		and self.date == lib:FormatDate(trade.datetime)
	then
		self:load()
	
		local min = int(trade.datetime.hour * 60 + trade.datetime.min)
		local d
		
		if #self.all_trades.min > 0 then
			d = self.all_trades.min[ self.all_trades.min[ #self.all_trades.min ] ]
		end
		
		if not d or d.min < min then
			if self.do_save then
				self:save()
			end
			
			d = {
					time = string.format("%02d%02d", trade.datetime.hour, trade.datetime.min),
					min = min,
					first = table_index,
					last = table_index
					--low = trade.price,
					--high = trade.price
				}
				
			table.sinsert(self.all_trades.min, tostring(min))			
			self.all_trades.min[tostring(min)] = d
			
		elseif d.min == min and d.last < table_index and not d.loaded then
			d.last = table_index
		end
	end
end


function index:update()
	if not self.table_index then
		self:load()
		if #self.all_trades.min > 0 then
			self.table_index = self.all_trades.min[ self.all_trades.min[ #self.all_trades.min ] ].last + 1
		else
			self.table_index = 0
		end
	end
	
	local trade
	
	while self.table_index < getNumberOf("all_trades") do
		trade = getItem("all_trades", self.table_index)
		if trade then
			self:AddTrade(trade, self.table_index)
		else
			return
		end
		self.table_index = self.table_index + 1
	end
end


function index:GetMin(min)
	self:update()
	if not self.all_trades.min then return end
	
	if type(min) == "string" then
		min = int(string.sub(min, 1, 2)) * 60 + int(string.sub(min, 3, 4))
	end
	
	if not min and #self.all_trades.min > 0 then
		min = self.all_trades.min[ #self.all_trades.min ]
	end
	
	if min then
		return self.all_trades.min[tostring(min)]
	end
end


end -- of file


